﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using WJP.Queue.Socketing;
using WJP.Queue.Socketing.BufferManagement;

namespace WJP.Queue.Remoting
{
    /// <summary>
    /// Socket远程服务管理
    /// </summary>
    public class SocketRemotingServer
    {
        #region 属性、字段

        private readonly Dictionary<int, IRequestHandler> _requestHandlerDict;
        private bool _isShuttingdown = true;
        private readonly SocketSetting _setting;

        public string Name { get; set; }
        public IBufferPool BufferPool { get; }

        /// <summary>
        /// Socket服务
        /// </summary>
        public ServerSocket ServerSocket { get; }

        #endregion

        #region 构造函数

        public SocketRemotingServer() : this("Server", new IPEndPoint(IPAddress.Loopback, 5000)) { }

        public SocketRemotingServer(string name, IPEndPoint listeningEndPoint, Action<SocketSetting> setting = null)
        {
            Name = name;
            _setting = new SocketSetting();
            setting.Invoke(_setting);
            BufferPool = new BufferPool(_setting.ReceiveDataBufferSize, _setting.ReceiveDataBufferPoolSize);
            //ServerSocket = new ServerSocket(name, listeningEndPoint, _setting, BufferPool, HandleRemotingRequest);
            _requestHandlerDict = new Dictionary<int, IRequestHandler>();
        }

        #endregion

        /// <summary>
        /// 启动Socket远程服务
        /// </summary>
        /// <returns></returns>
        public SocketRemotingServer Start()
        {
            _isShuttingdown = false;
            ServerSocket.Start();
            return this;
        }

        /// <summary>
        /// 关闭Socket远程服务
        /// </summary>
        /// <returns></returns>
        public SocketRemotingServer Shutdown()
        {
            _isShuttingdown = true;
            ServerSocket.Shutdown();
            return this;
        }
    }
}
